<template>
  <el-dialog
    :title="!dataForm.id ? $t(`role.addRole`) : $t(`role.editRole`)"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      v-loading="formLoading"
      label-width="100px"
    >
      <el-form-item label="角色名称" prop="fullName">
        <el-input
          v-model="dataForm.fullName"
          placeholder="输入角色名称"
          maxlength="50"
        />
      </el-form-item>
      <el-form-item label="角色编码" prop="enCode">
        <el-input v-model="dataForm.enCode" placeholder="输入角色编码" />
      </el-form-item>
      <el-form-item label="角色类型" prop="globalMark">
        <el-select
          v-model="dataForm.globalMark"
          placeholder="请选择角色类型"
          @change="dataForm.organizeIdsTree = []"
        >
          <el-option label="全局" :value="1" v-if="organizeId == 2"></el-option>
          <el-option label="组织" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色分类" prop="typeList">
        <el-select
          v-model="dataForm.typeList"
          placeholder="请选择角色分类"
          multiple
        >
          <el-option
            v-for="(item, index) in typeOptions"
            :key="index"
            :label="item.fullName"
            :value="item.enCode"
            :disabled="item.disabled"
          ></el-option>
          <!-- <el-option label="全局" :value="1" v-if="organizeId == 2"></el-option>
          <el-option label="组织" :value="0"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item
        label="所属组织"
        prop="organizeIdsTree"
        v-if="dataForm.globalMark === 0"
      >
        <ComSelect
          v-model="dataForm.organizeIdsTree"
          placeholder="选择所属组织"
          multiple
          clearable
        />
      </el-form-item>
      <el-form-item label="排序" prop="sortCode">
        <el-input-number
          :min="0"
          :max="999999"
          v-model="dataForm.sortCode"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="状态" prop="enabledMark">
        <el-switch
          v-model="dataForm.enabledMark"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <el-input v-model="dataForm.description" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{
        $t("common.cancelButton")
      }}</el-button>
      <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">
        {{ $t("common.confirmButton") }}</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getRoleInfo, createRole, updateRole,copyRole } from "@/api/permission/role";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import { isEmpty } from "@/utils/isEmpty";

export default {
  data() {
    return {
      typeOptions: [],
      visible: false,
      formLoading: false,
      btnLoading: false,
      isCopy: false,
      organizeId: "",
      dataForm: {
        fullName: "",
        enCode: "",
        type: "",
        enabledMark: 1,
        globalMark: null,
        organizeIdsTree: [],
        sortCode: 0,
        description: "",
        typeList: [],
      },
      dataRule: {
        fullName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          {
            validator: this.formValidate(
              "fullName",
              "角色名称不能含有特殊符号"
            ),
            trigger: "blur",
          },
          { max: 50, message: "角色名称最多为50个字符！", trigger: "blur" },
        ],
        enCode: [
          { required: true, message: "请输入角色编码", trigger: "blur" },
          {
            validator: this.formValidate(
              "enCode",
              "角色编码只能输入英文、数字和小数点且小数点不能放在首尾"
            ),
            trigger: "blur",
          },
          { max: 50, message: "角色编码最多为50个字符！", trigger: "blur" },
        ],
        globalMark: [
          { required: true, message: "请选择角色类型", trigger: "change" },
        ],
        organizeIdsTree: [
          {
            required: true,
            message: "请选择所属组织",
            trigger: "change",
            type: "array",
          },
        ],
      },
    };
  },
  methods: {
    gettypeOptions() {
      getDictionaryDataSelector("4501f6f26a384757bce12d4c4b03342c").then(
        (res) => {
          this.typeOptions = res.data.list;
        }
      );
    },
    init(id) {
      var userInfo = this.$store.getters.userInfo;
      this.organizeId = userInfo.organizeId;
      this.dataForm.id = id || "";
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.formLoading = true;
          getRoleInfo(this.dataForm.id).then((res) => {
            this.dataForm = res.data;
            if(this.isCopy){
              this.dataForm.organizeIdsTree=[]
            }
            // this.dataForm.type = this.dataForm.typeList.split(",");
            if (!isEmpty(this.dataForm.type)) {
              this.dataForm.typeList = this.dataForm.type.split(",");
            }
            this.formLoading = false;
          });
        }
      });
      this.gettypeOptions();
    },
    initCopy(id) {
      this.isCopy = true
      this.init(id);

    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (!isEmpty(this.dataForm.typeList)) {
            this.dataForm.type = this.dataForm.typeList.join(",");
          } else {
            this.dataForm.type = "";
          }
          this.btnLoading = true;

          const formMethodOper = this.dataForm.id ? updateRole : createRole
          const formMethod=this.isCopy?copyRole:formMethodOper
          formMethod(this.dataForm)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.btnLoading = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {
              this.btnLoading = false;
            });
        }
      });
    },
  },
};
</script>
